package presstrack
{
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.*;
	
	[RemoteClass(alias="presstrack.PaperDeliveryClass")]
	[Bindable]
	public class PaperDeliveryClass
	{
		public static var DEL_YEAR:Number				= new Date().getFullYear();
		public static var DEL_MONTH:Number				= new Date().getMonth() + 1;
		
		public static var DEL_START_DATE:String			= new String();
		public static var DEL_END_DATE:String			= new String();
		
		public static var DEL_PAPERS:ArrayCollection	= new ArrayCollection();
		public static var DEL_SUPPLIERS:ArrayCollection	= new ArrayCollection();
		public static var DEL_PAYERS:ArrayCollection	= new ArrayCollection();
		
		// public
		public var iDeliveryId:int;
		public var mInvoice:String; 
		public var mCreationDate:String; 
		
		public var iPaperId:int;
		public var mPaperLabel:String; 
		public var iPaperThick:int; 
		public var mPaperSize:String;		
		
		public var mSupplier:String; 
		public var mPayer:String; 
		public var mSum:String; 
		public var iNum:int; 	
					
		public function PaperDeliveryClass(ob:Object = null):void
		{
			iDeliveryId		= -1;
			mInvoice		= "?"; 
			mCreationDate	= "??/??/????"; 
			
			iPaperId		= -1;
			mPaperLabel		= "?"; 
			iPaperThick		= 0; 
			mPaperSize		= "?";		
		
			mSupplier		= ""; 
			mPayer			= ""; 
			mSum			= "0.00"; 
			iNum			= 0; 
		
			if (ob != null) 
			{
				copyFrom(ob);
			}
		}
		
		public function copyFrom(ob:Object):void
		{
			if (ob.hasOwnProperty("iddelivery"))
			{
				iDeliveryId		= ob.iddelivery;	
			}			
			
			if (ob.hasOwnProperty("invoice"))
			{
				mInvoice		= ob.invoice.toString();
			}				
			
			if (ob.hasOwnProperty("creation"))
			{
				mCreationDate	= ob.creation.toString();
			}			
			
			if (ob.hasOwnProperty("paper"))
			{
				iPaperId		= ob.paper;	
			}
			
			if (ob.hasOwnProperty("label"))
			{
				mPaperLabel		= ob.label;	
			}
			
			if (ob.hasOwnProperty("thick"))
			{
				iPaperThick		= ob.thick;	
			}
			
			if (ob.hasOwnProperty("size"))
			{
				mPaperSize		= ob.size;	
			}
			
			if (ob.hasOwnProperty("sum"))
			{
				mSum = Number(ob.sum).toFixed(2);
			}
			
			if (ob.hasOwnProperty("num"))
			{
				iNum 			= ob.num;
			}				
			
			if (ob.hasOwnProperty("supplier"))
			{
				mSupplier 		= ob.supplier.toString();
			}
			
			if (ob.hasOwnProperty("payer") && ob.payer != null)
			{
				mPayer 			= ob.payer.toString();
			}			
		}
		
		public function setInvoice(value		:String, 
								   resFunc		:Function	= null, 
								   faultFunc	:Function	= null):void
		{
			updateParam("invoice", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mInvoice = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setCreation(value		:String, 
								    resFunc		:Function	= null, 
									faultFunc	:Function	= null):void
		{
			updateParam("creation", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mCreationDate = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setPaper(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			updateParam("paper", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					iPaperId = int(value);
					CrudService.crud.getPapers(resFunc, faultFunc);
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setNum(value		:String, 
							   resFunc		:Function	= null, 
							   faultFunc	:Function	= null):void
		{
			updateParam("num", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					iNum = int(value);
					CrudService.crud.getPapers(resFunc, faultFunc);
					if (resFunc != null) 
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null) 
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setSupplier(value		:String, 
									resFunc		:Function	= null, 
									faultFunc	:Function	= null):void
		{
			updateParam("supplier", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mSupplier = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setPayer(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			updateParam("payer", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mPayer = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setSum(value		:String, 
							   resFunc		:Function	= null, 
							   faultFunc	:Function	= null):void
		{
			updateParam("sum", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mSum = Number(value).toFixed(2);
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		// private
		
		private function updateParam(param		:String, 
									 value		:String, 
									 onResult	:Function	= null, 
									 faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.UpdateDelivery", new Responder(onResult, onFault), 
									  iDeliveryId, param, value);
									
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}			
		}		
	}
}